package com.dhkj.circle.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dhkj.circle.R;
import com.dhkj.circle.broadcast.TimeEvent;
import com.dhkj.circle.constant.CardConst;
import com.dhkj.circle.db.sqlite.InfoDBSQLite;
import com.dhkj.circle.entity.CardInfo;
import com.dhkj.circle.impl.LSZMWXImpl;
import com.dhkj.circle.ui.dialog.ZMWXDialog;
import com.dhkj.circle.ui.dialog.ZMWXOkDialog;
import com.dhkj.circle.utils.DateUtils;
import com.dhkj.circle.utils.NFCUtils;
import com.dhkj.circle.utils.QMUIView;
import com.dhkj.circle.utils.SystemUIUtils;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.dhkj.circle.ui.AppContext.sLogger;

public class IndexActivity extends Activity {
    private int count = 0;

    @BindView(R.id.name)
    TextView name;

    private QMUIDialog qmuiDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        count = 0;
        setContentView(R.layout.activity_main);
        QMUIDisplayHelper.setFullScreen(this);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        name.setText("姓名：" + NFCUtils.getCardInfo().name);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        SystemUIUtils.setStickFullScreen(getWindow().getDecorView());
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onMessageEvent(TimeEvent timeEvent) {

        if (!IndexActivity.this.isFinishing()) {
            int status = NFCUtils.readNFC(this.getLocalClassName());
            if (status == 1) {
                CardInfo cardInfo = NFCUtils.getCardInfo();
                if (cardInfo.zyh == null) {
                    if (count > 1) {
                        finish();
                    }
                    count++;
                }
            } else if (status != 10) {
                if (count > 1) {
                    finish();
                }
                count++;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NFCUtils.remove(this.getLocalClassName());
        if (qmuiDialog != null && qmuiDialog.isShowing()) {
            qmuiDialog.dismiss();
        }
        QMUIView.loadingClose();
        EventBus.getDefault().unregister(this);
    }

    @butterknife.OnClick({R.id.checkbox, R.id.fanka_box})
    public void onViewClicked(View view) {
        LSZMWXImpl.getInstance().setActivity(this);
        switch (view.getId()) {

            case R.id.checkbox:
                sLogger.d("进行查询操作:" + String.valueOf(CardConst.getCardInfo().zyh));
                QMUIView.loadingShow(this, "正在查询");
                NFCUtils.readNFC(this.getLocalClassName());
                CardConst.setCardInfo(NFCUtils.getCardInfo());
                LSZMWXImpl.getInstance().getUserJeByCardNo(String.valueOf(CardConst.getCardInfo().zyh).trim());
                LSZMWXImpl.setLszmwXshowListener((s, je) -> {
                    if (s.equals("2")) {
                        StringBuffer stringBuffer = new StringBuffer();
                        stringBuffer.append("学号：" + String.valueOf(NFCUtils.getCardInfo().zyh) + "\n");
                        stringBuffer.append("姓名：" + NFCUtils.getCardInfo().name + "\n\n");
                        stringBuffer.append("饭卡\n");
                        stringBuffer.append("卡上金额：" + BigDecimal.valueOf(NFCUtils.getCardInfo().je).divide(BigDecimal.valueOf(100)) + "\t元\n");
                        stringBuffer.append("待充值金额：" + je + "\t元\n\n");
                        qmuiDialog = new QMUIDialog.MessageDialogBuilder(this)
                                .setTitle("查询")
                                .setMessage(stringBuffer.toString())
                                .addAction("返回", (QMUIDialog dialog, int index) -> dialog.dismiss())
                                .show();
                    }
                });
                QMUIView.loadingClose();
                break;
            case R.id.fanka_box:
                QMUIView.loadingShow(this, "正在充值......\n\n请勿移走卡片");
                LSZMWXImpl.getInstance().getUserJeByCardNo2(String.valueOf(CardConst.getCardInfo().zyh).trim());


                LSZMWXImpl.setLszmwXshowListener((s, je) -> {
                    if (s.equals("1")) {
                        QMUIView.loadingClose();
                        ZMWXDialog zmwxDialog = new ZMWXDialog();
                        zmwxDialog.show(getFragmentManager(), "zmwxDialog");

                    }
                });


                LSZMWXImpl.setLszmwxWriteListener(() -> {
                    try {
                        NFCUtils.readNFC(this.getLocalClassName());
                        CardInfo cardInfo = NFCUtils.getCardInfo();
                        if (cardInfo != null && (String.valueOf(cardInfo.zyh).trim().equals(String.valueOf(CardConst.getCardInfo().zyh).trim()))) {
                            sLogger.d("开始写卡");
                            BigDecimal je = BigDecimal.valueOf(cardInfo.je).add((BigDecimal.valueOf(Double.parseDouble(CardConst.getJe().trim())).multiply(BigDecimal.valueOf(100))));
                            cardInfo.je = je.intValue();
                            NFCUtils.writeNFC(this.getLocalClassName(), cardInfo);
                            ZMWXOkDialog zmwxOkDialog = new ZMWXOkDialog();
                            zmwxOkDialog.show(getFragmentManager(), "zmwxOkDialog");
                            QMUIView.loadingClose();
                        } else {
                            QMUIView.showError(this, "请不要换卡，充值失败");
                        }
                    } catch (Exception e) {
                        sLogger.e(e.getMessage());
                    }
                });

                break;
        }
    }


}
